package com.chenwenxuan.sync.console.task;

import com.chenwenxuan.sync.console.model.Task;

public class ExecuteTaskFactory {

	public static ExecuteTask create(Task task) {
		String taskType = task.getTaskType();

		try {

			Class<?> c = Class.forName(taskType);
			Object newTask = c.newInstance();

			if (newTask instanceof ExecuteTask) {
				ExecuteTask t = (ExecuteTask) newTask;
				t.setTask(task);
				return t;
			} else {
				throw new ClassCastException(task.getTaskType()
						+ "is not a subclass of "
						+ ExecuteTask.class.getCanonicalName());
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassCastException e) {
			e.printStackTrace();
		}

		return null;

	}

}
